<template>
  <div class="node-palette">
    <div class="palette-header">
      <n-h3 style="margin: 0; font-size: 16px;">电缆组件</n-h3>
      <n-input 
        v-model:value="searchKeyword" 
        placeholder="搜索组件..." 
        clearable
        size="small"
      />
    </div>
    
    <div class="palette-content">
      <n-scrollbar style="max-height: 100%;">
        <div class="node-list">
          <div
            v-for="node in filteredNodes"
            :key="node.type"
            class="node-item"
            draggable="true"
            @dragstart="(e) => handleDragStart(e, node)"
            @click="$emit('node-select', node.type)"
          >
            <div class="node-icon" :style="{ backgroundColor: node.color }">
              <n-icon :component="node.icon" :size="16" />
            </div>
            <div class="node-name">{{ node.name }}</div>
          </div>
        </div>
      </n-scrollbar>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { NScrollbar, NInput, NH3, NIcon } from 'naive-ui'

const props = defineProps({
  nodeTemplates: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['node-select'])

const searchKeyword = ref('')

// 所有节点平铺显示，不再分类
const filteredNodes = computed(() => {
  const allNodes = Object.values(props.nodeTemplates)
  if (!searchKeyword.value) return allNodes
  return allNodes.filter(node => 
    node.name.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
    node.description.toLowerCase().includes(searchKeyword.value.toLowerCase())
  )
})

// 拖拽处理
const handleDragStart = (event, node) => {
  event.dataTransfer.setData('application/vueflow/node', JSON.stringify(node))
  event.dataTransfer.effectAllowed = 'move'
  emit('node-select', node.type)
}
</script>

<style scoped>
.node-palette {
  width: 100%;
  height: 100%;
  background: white;
  display: flex;
  flex-direction: column;
}

.palette-header {
  padding: 12px;
  border-bottom: 1px solid #e8e8e8;
  flex-shrink: 0; /* 防止头部被压缩 */
}

.palette-content {
  flex: 1;
  min-height: 0; /* 重要：允许内容区域收缩 */
  overflow: hidden; /* 隐藏外层滚动条 */
}

.node-list {
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.node-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px;
  border: 1px solid #e8e8e8;
  border-radius: 6px;
  background: white;
  cursor: grab;
  transition: all 0.2s;
  min-height: 60px;
  justify-content: center;
}

.node-item:hover {
  border-color: #2080f0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.node-icon {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
  color: white;
}

.node-name {
  font-size: 12px;
  font-weight: 500;
  text-align: center;
  line-height: 1.2;
}
</style>